﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Koonsoft.CustomerService.Entity;
using System.Data;

namespace Koonsoft.CustomerService.DAL
{
    public class WFDAL
    {
        private static Database database = DatabaseFactory.CreateDatabase("CustomerService");

        private static List<WF> wfs;

        private static List<WF> Wfs
        {
            get
            {
                if (wfs==null) 
                {
                    wfs = GetWf();
                }
                return WFDAL.wfs;
            }            
        }


        private static List<WF> GetWf()
        {
            const string sql = @"select * From wf order by wfid";
            List<WF> _wfs = new List<WF>();
            try
            {
                using (var cmd = database.GetSqlStringCommand(sql))
                {
                  

                    DataTable dt = database.ExecuteDataSet(cmd).Tables[0];

                    foreach (DataRow dr in dt.Rows)
                    {
                        WF wf = new WF();
                        wf.WFID = Convert.ToInt32(dr["WFID"]);
                        wf.State = dr["State"].ToString();
                        wf.NextLink = dr["NextLink"].ToString();
                        _wfs.Add(wf);
                    }

                    return _wfs;
                }
            }
            catch (Exception ex)
            {
            }
            return new List<WF>();
        }


        public WF GetFirstWF(out string msg)
        {
            //msg = "";

            //const string sql = @"select top 1 * From wf order by wfid";

            //try
            //{
            //    using (var cmd = database.GetSqlStringCommand(sql))
            //    {
            //        WF wf = new WF();

            //        DataTable dt = database.ExecuteDataSet(cmd).Tables[0];

            //        if (dt.Rows.Count > 0)
            //        {
            //            wf.WFID = Convert.ToInt32(dt.Rows[0]["WFID"]);
            //            wf.State = dt.Rows[0]["State"].ToString();
            //        }

            //        return wf;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    msg = ex.Message + ";" + ex.StackTrace;
            //}
            //return new WF();

            msg = "";
            return Wfs.OrderBy(fun => fun.WFID).FirstOrDefault();
        }

        public WF GetWfById(int id)
        {
            return Wfs.Where(fun => fun.WFID == id).FirstOrDefault();
        }

        public WF GetNextWf(int id)
        {
            return Wfs.Where(fun => fun.WFID > id).OrderBy(fun=>fun.WFID).FirstOrDefault();
        }

        public WF GetWfByStateName(string stateName)
        {
            return Wfs.Where(fun => fun.State == stateName).OrderBy(fun => fun.WFID).FirstOrDefault();
        }
    }
}
